﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using cndp.Model;
using iTextSharp.text;
using iTextSharp.text.pdf;
using log4net;
using log4net.Config;
using Font = iTextSharp.text.Font;

namespace cndp.View
{
    public partial class FormStartEvent : Form
    {
        private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private readonly FormShowHighscore _fsh = new FormShowHighscore();
        private int _rowOfEvent = -1;
        public string ContestName;
        public string Date;
        public BindingList<Event> EventList = new BindingList<Event>();
        public BindingList<Judge> JudgeRegister = new BindingList<Judge>();

        public FormStartEvent()
        {
            InitializeComponent();
            InitializeHighScoreLabels();
            InitializeEventJumpLabels();
            _fsh.InitializeHighScoreLabels();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewEventList.Rows.Count != 0)
            {
                _rowOfEvent = e.RowIndex;
                UpdateEventJumpsLabels();
                UpdateHighScoreLabels();
            }
        }

        private void InitializeEventJumpLabels()
        {
            labelDiverNameCurrent.Text = @"";
            labelStyleCurrent.Text = @"";
            labelJumpCurrent.Text = @"";
            labelDifficultyCurrent.Text = @"";
            labelJudgePointResult.Text = @"";

            labelOrdervaluenext.Text = @" ";
            labelJumpstylenext.Text = @" ";
            labelJumpIdnext.Text = @" ";
            labelDivernamenext.Text = @" ";
            labelRoundnext.Text = @" ";

            labelOrdervalue0.Text = @" ";
            labelDiverName0.Text = @" ";
            labelRound0.Text = @" ";

            labelOrdervalue1.Text = @" ";
            labelDiverName1.Text = @" ";
            labelRound1.Text = @" ";

            labelOrdervalue2.Text = @" ";
            labelDiverName2.Text = @" ";
            labelRound2.Text = @" ";

            labelOrdervalue3.Text = @" ";
            labelDiverName3.Text = @" ";
            labelRound3.Text = @" ";

            labelOrdervalue4.Text = @" ";
            labelDiverName4.Text = @" ";
            labelRound4.Text = @" ";

            labelOrdervalue5.Text = @" ";
            labelDiverName5.Text = @" ";
            labelRound5.Text = @" ";

            labelOrdervalue6.Text = @" ";
            labelDiverName6.Text = @" ";
            labelRound6.Text = @" ";

            labelOrdervalue7.Text = @" ";
            labelDiverName7.Text = @" ";
            labelRound7.Text = @" ";

            labelOrdervalue8.Text = @" ";
            labelDiverName8.Text = @" ";
            labelRound8.Text = @" ";

            labelOrdervalue9.Text = @" ";
            labelDiverName9.Text = @" ";
            labelRound9.Text = @" ";

            textBoxD4.BackColor = SystemColors.ScrollBar;
            textBoxD5.BackColor = SystemColors.ScrollBar;
            textBoxD6.BackColor = SystemColors.ScrollBar;
            textBoxD7.BackColor = SystemColors.ScrollBar;
            textBoxD8.BackColor = SystemColors.ScrollBar;
            textBoxD9.BackColor = SystemColors.ScrollBar;
            textBoxD10.BackColor = SystemColors.ScrollBar;
            textBoxD11.BackColor = SystemColors.ScrollBar;

            textBoxD1.ReadOnly = true;
            textBoxD2.ReadOnly = true;
            textBoxD3.ReadOnly = true;
            textBoxD4.ReadOnly = true;
            textBoxD5.ReadOnly = true;
            textBoxD6.ReadOnly = true;
            textBoxD7.ReadOnly = true;
            textBoxD8.ReadOnly = true;
            textBoxD9.ReadOnly = true;
            textBoxD10.ReadOnly = true;
            textBoxD11.ReadOnly = true;
        }

        private void UpdateEventJumpsLabels()
        {
            var tempTextBoxList = new BindingList<TextBox>();
            tempTextBoxList.Add(textBoxD1);
            tempTextBoxList.Add(textBoxD2);
            tempTextBoxList.Add(textBoxD3);
            tempTextBoxList.Add(textBoxD4);
            tempTextBoxList.Add(textBoxD5);
            tempTextBoxList.Add(textBoxD6);
            tempTextBoxList.Add(textBoxD7);
            tempTextBoxList.Add(textBoxD8);
            tempTextBoxList.Add(textBoxD9);
            tempTextBoxList.Add(textBoxD10);
            tempTextBoxList.Add(textBoxD11);

            for (var i = 0; i < 11; i++)
            {
                if (EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].JudgePoints.Count >= i + 1)
                    tempTextBoxList[i].Text =
                        EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].JudgePoints[i].ToString();
                else
                    tempTextBoxList[i].Text = "";
            }

            labelDiverNameCurrent.Text = EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].DiverName;
            labelStyleCurrent.Text = EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].Style;
            labelJumpCurrent.Text =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].JumpId.ToString();
            labelDifficultyCurrent.Text =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].Difficulty.ToString();
            labelJudgePointResult.Text =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].Result.ToString();

            if ((EventList[_rowOfEvent].CurrentDiver + 1) < EventList[_rowOfEvent].JumpList.Count)
            {
                labelOrdervaluenext.Text = (EventList[_rowOfEvent].CurrentDiver + 1).ToString();
                labelJumpstylenext.Text = EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + 1].Style;
                labelJumpIdnext.Text =
                    EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + 1].JumpId.ToString();
                labelDivernamenext.Text =
                    EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + 1].DiverName;
                labelRoundnext.Text =
                    EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + 1].Round.ToString();
            }
            else
            {
                labelOrdervaluenext.Text = @" ";
                labelStyleNext.Text = @" ";
                labelJumpIdnext.Text = @" ";
                labelDivernamenext.Text = @" ";
                labelRoundnext.Text = @" ";
            }

            var tempOrderValueList = new BindingList<Label>();
            tempOrderValueList.Add(labelOrdervalue0);
            tempOrderValueList.Add(labelOrdervalue1);
            tempOrderValueList.Add(labelOrdervalue2);
            tempOrderValueList.Add(labelOrdervalue3);
            tempOrderValueList.Add(labelOrdervalue4);
            tempOrderValueList.Add(labelOrdervalue5);
            tempOrderValueList.Add(labelOrdervalue6);
            tempOrderValueList.Add(labelOrdervalue7);
            tempOrderValueList.Add(labelOrdervalue8);
            tempOrderValueList.Add(labelOrdervalue9);

            var tempDiverNameList = new BindingList<Label>();
            tempDiverNameList.Add(labelDiverName0);
            tempDiverNameList.Add(labelDiverName1);
            tempDiverNameList.Add(labelDiverName2);
            tempDiverNameList.Add(labelDiverName3);
            tempDiverNameList.Add(labelDiverName4);
            tempDiverNameList.Add(labelDiverName5);
            tempDiverNameList.Add(labelDiverName6);
            tempDiverNameList.Add(labelDiverName7);
            tempDiverNameList.Add(labelDiverName8);
            tempDiverNameList.Add(labelDiverName9);

            var tempRoundList = new BindingList<Label>();
            tempRoundList.Add(labelRound0);
            tempRoundList.Add(labelRound1);
            tempRoundList.Add(labelRound2);
            tempRoundList.Add(labelRound3);
            tempRoundList.Add(labelRound4);
            tempRoundList.Add(labelRound5);
            tempRoundList.Add(labelRound6);
            tempRoundList.Add(labelRound7);
            tempRoundList.Add(labelRound8);
            tempRoundList.Add(labelRound9);

            for (var i = 0; i <= 9; i++)
            {
                if ((EventList[_rowOfEvent].CurrentDiver + (i + 2)) < EventList[_rowOfEvent].JumpList.Count)
                {
                    tempOrderValueList[i].Text = (EventList[_rowOfEvent].CurrentDiver + (i + 2)).ToString();
                    tempDiverNameList[i].Text =
                        EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + (i + 2)].DiverName;
                    tempRoundList[i].Text =
                        EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + (i + 2)].Round.ToString();
                }
                else
                {
                    tempOrderValueList[i].Text = @" ";
                    tempDiverNameList[i].Text = @" ";
                    tempRoundList[i].Text = @" ";
                }
            }

            for (var i = 5; i <= 11; i += 2)
            {
                if (EventList[_rowOfEvent].MaxJudges >= i)
                {
                    tempTextBoxList[i - 2].BackColor = SystemColors.Window;
                    tempTextBoxList[i - 1].BackColor = SystemColors.Window;
                }
            }
        }

        private void InitializeHighScoreLabels()
        {
            labelHSManOrWomen.Text = @"";
            labelHSheight.Text = @"";
            labelHSklass.Text = @"";

            labelHSdivername1.Text = @"";
            labelHSResult1.Text = @"";

            labelHSdivername2.Text = @"";
            labelHSResult2.Text = @"";
            labelHSdif2.Text = @"";

            labelHSdivername3.Text = @" ";
            labelHSResult3.Text = @" ";
            labelHSdif3.Text = @" ";

            labelHSdivername4.Text = @" ";
            labelHSResult4.Text = @" ";
            labelHSdif4.Text = @" ";

            labelHSdivername5.Text = @" ";
            labelHSResult5.Text = @" ";
            labelHSdif5.Text = @" ";

            labelHSdivername6.Text = @" ";
            labelHSResult6.Text = @" ";
            labelHSdif6.Text = @" ";

            labelHSdivername7.Text = @" ";
            labelHSResult7.Text = @" ";
            labelHSdif7.Text = @" ";

            labelHSdivername8.Text = @" ";
            labelHSResult8.Text = @" ";
            labelHSdif8.Text = @" ";

            labelHSdivername9.Text = @" ";
            labelHSResult9.Text = @" ";
            labelHSdif9.Text = @" ";

            labelHSdivername10.Text = @" ";
            labelHSResult10.Text = @" ";
            labelHSdif10.Text = @" ";
        }

        private void UpdateHighScoreLabels()
        {
            labelHSManOrWomen.Text = EventList[_rowOfEvent].Gender;
            labelHSheight.Text = EventList[_rowOfEvent].JumpHeight + @" meter";
            labelHSklass.Text = EventList[_rowOfEvent].Bracket;

            if (EventList[_rowOfEvent].CoupleBool)
                labelHSSingleOrCouple.Text = @"Parhopp";
            else
                labelHSSingleOrCouple.Text = @"Singelhopp";

            var tempDiverNameList = new BindingList<Label>();
            tempDiverNameList.Add(labelHSdivername2);
            tempDiverNameList.Add(labelHSdivername3);
            tempDiverNameList.Add(labelHSdivername4);
            tempDiverNameList.Add(labelHSdivername5);
            tempDiverNameList.Add(labelHSdivername6);
            tempDiverNameList.Add(labelHSdivername7);
            tempDiverNameList.Add(labelHSdivername8);
            tempDiverNameList.Add(labelHSdivername9);
            tempDiverNameList.Add(labelHSdivername10);

            var tempResultList = new BindingList<Label>();
            tempResultList.Add(labelHSResult2);
            tempResultList.Add(labelHSResult3);
            tempResultList.Add(labelHSResult4);
            tempResultList.Add(labelHSResult5);
            tempResultList.Add(labelHSResult6);
            tempResultList.Add(labelHSResult7);
            tempResultList.Add(labelHSResult8);
            tempResultList.Add(labelHSResult9);
            tempResultList.Add(labelHSResult10);

            var tempDifList = new BindingList<Label>();
            tempDifList.Add(labelHSdif2);
            tempDifList.Add(labelHSdif3);
            tempDifList.Add(labelHSdif4);
            tempDifList.Add(labelHSdif5);
            tempDifList.Add(labelHSdif6);
            tempDifList.Add(labelHSdif7);
            tempDifList.Add(labelHSdif8);
            tempDifList.Add(labelHSdif9);
            tempDifList.Add(labelHSdif10);

            labelHSdivername1.Text = EventList[_rowOfEvent].HighScore[0].Key.Name;
            labelHSResult1.Text = EventList[_rowOfEvent].HighScore[0].Value.ToString();

            for (var i = 0; i <= 8; i++)
            {
                if (EventList[_rowOfEvent].HighScore.Count >= (i + 2))
                {
                    tempDiverNameList[i].Text = EventList[_rowOfEvent].HighScore[i + 1].Key.Name;
                    tempResultList[i].Text = Math.Round(EventList[_rowOfEvent].HighScore[i + 1].Value, 1).ToString();
                    tempDifList[i].Text = @"-" +
                                          Math.Round(
                                              (EventList[_rowOfEvent].HighScore[0].Value -
                                               EventList[_rowOfEvent].HighScore[i + 1].Value), 1);
                }
            }
        }

        private void FormStartEvent_Load(object sender, EventArgs e)
        {
            dataGridViewEventList.DataSource = EventList;
            dataGridViewEventList.Rows[0].Selected = true;
            dataGridViewEventList.Enabled = false;
            KeyPreview = true;
            _rowOfEvent = 0;
            EventList[_rowOfEvent].UpdateJumpcodes();
            SetJumpDifficulties();
            UpdateEventJumpsLabels();
            UpdateHighScoreLabels();

            progressBar.Maximum = EventList[_rowOfEvent].JumpList.Count;
            progressBar.Value = EventList[_rowOfEvent].CurrentDiver;

            var t = new Thread(GetPoints); //Gör ett event som anropar konstruktorn.
            t.IsBackground = true;
            t.Start();

            #region labelinvisible

            labelInvisible0.Visible = false;
            labelInvisible1.Visible = false;
            labelInvisible2.Visible = false;
            labelInvisible3.Visible = false;
            labelInvisible4.Visible = false;
            labelInvisible5.Visible = false;
            labelInvisible6.Visible = false;
            labelInvisible7.Visible = false;
            labelInvisible8.Visible = false;
            labelInvisible9.Visible = false;
            labelInvisible10.Visible = false;
            labelInvisible11.Visible = false;
            labelInvisible12.Visible = false;
            labelInvisible13.Visible = false;
            labelInvisible14.Visible = false;
            labelInvisible15.Visible = false;
            labelInvisible16.Visible = false;
            labelInvisible17.Visible = false;
            labelInvisible18.Visible = false;
            labelInvisible19.Visible = false;
            labelInvisible20.Visible = false;
            labelInvisible21.Visible = false;
            labelInvisible22.Visible = false;
            labelInvisible23.Visible = false;
            labelinvisibleP.Visible = false;

            #endregion
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _fsh.Show();
        }

        private void buttonNextEventjump_Click(object sender, EventArgs e)
        {
            buttonGetResult.PerformClick();
            if (CheckTextboxes())
            {
                EventList[_rowOfEvent].NextJumpInJumpList();
                EventList[_rowOfEvent].UpdateHighscore();
                EventList[_rowOfEvent].SortHighscore();

                if (EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver - 1].Round !=
                    EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].Round)
                    EventList[_rowOfEvent].SortJumplistByPoints(
                        EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].Round);

                TextBox[] textBoxArray =
                {
                    textBoxD1, textBoxD2, textBoxD3, textBoxD4, textBoxD5, textBoxD6, textBoxD7,
                    textBoxD8, textBoxD9, textBoxD10, textBoxD11
                };
                for (var i = 0; i < EventList[_rowOfEvent].MaxJudges; ++i)
                {
                    textBoxArray[i].ReadOnly = true;
                }

                progressBar.Value = EventList[_rowOfEvent].CurrentDiver;
                UpdateEventJumpsLabels();

                UpdateHighScoreLabels();
                _fsh.UpdateHighScoreLabels(EventList, _rowOfEvent);
                Voted.Clear();
            }
        }

        private void buttonLastEventjump_Click(object sender, EventArgs e)
        {
            EventList[_rowOfEvent].PreviousJumpInJumpList();
            UpdateEventJumpsLabels();
            UpdateHighScoreLabels();
            _fsh.UpdateHighScoreLabels(EventList, _rowOfEvent);
            progressBar.Value = EventList[_rowOfEvent].CurrentDiver;
            TextBox[] textBoxArray =
            {
                textBoxD1, textBoxD2, textBoxD3, textBoxD4, textBoxD5, textBoxD6, textBoxD7,
                textBoxD8, textBoxD9, textBoxD10, textBoxD11
            };

            for (var i = 0; i < EventList[_rowOfEvent].MaxJudges; ++i)
                textBoxArray[i].ReadOnly = true;
        }

        private void buttonGetJudgepoints_Click(object sender, EventArgs e)
        {
            UpdateEventJumpsLabels();
            TextBox[] textBoxArray =
            {
                textBoxD1, textBoxD2, textBoxD3, textBoxD4, textBoxD5, textBoxD6, textBoxD7,
                textBoxD8, textBoxD9, textBoxD10, textBoxD11
            };

            for (var i = 0; i < EventList[_rowOfEvent].MaxJudges; ++i)
            {
                textBoxArray[i].ReadOnly = false;
            }
        }

        private void buttonGetResult_Click(object sender, EventArgs e)
        {
            TextBox[] textBoxArray =
            {
                textBoxD1, textBoxD2, textBoxD3, textBoxD4, textBoxD5, textBoxD6, textBoxD7,
                textBoxD8, textBoxD9, textBoxD10, textBoxD11
            };
            if (CheckTextboxes())
            {
                for (var i = 0; i < textBoxArray.Count(); ++i)
                {
                    if (EventList[_rowOfEvent].JudgeList.Count >= i + 1)
                    {
                        if (EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].JudgePoints.Count <
                            i + 1)
                            EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].AddJudgePoint(
                                double.Parse(textBoxArray[i].Text), EventList[_rowOfEvent]);

                        else
                            EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].ChangeJudgePoint(i,
                                double.Parse(textBoxArray[i].Text));
                    }
                }
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].CalculateResult(
                    EventList[_rowOfEvent]);
                labelJudgePointResult.Text =
                    EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].Result.ToString();
            }
            else
                MessageBox.Show(@"Samtliga domare måste ge poäng");
        }

        private void buttonChangeJumpcodeCurrent_Click(object sender, EventArgs e)
        {
            var formSetJumpCode = new FormSetJumpcodes();
            formSetJumpCode.TotalAmountOfRounds = EventList[_rowOfEvent].Rounds;
            formSetJumpCode.CurrentRound = EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].Round;
            formSetJumpCode.labelName.Text =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].DiverName;
            formSetJumpCode.labelPersonalCodeNumber.Text =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].DiverId;
            formSetJumpCode.JumpAppendixList = EventList[_rowOfEvent].Jumpappendix;
            formSetJumpCode.JumpHeight = EventList[_rowOfEvent].JumpHeight;

            for (var i = 0; i < EventList[_rowOfEvent].DiverList.Count; ++i)
            {
                if (EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver].DiverId ==
                    EventList[_rowOfEvent].DiverList[i].Diver.PersonalCodeNumber)
                {
                    formSetJumpCode.ListOfJumpCodesAndStyles = EventList[_rowOfEvent].DiverList[i].JumpCodesandStyle;
                    i = EventList[_rowOfEvent].DiverList.Count + 1;
                }
            }

            formSetJumpCode.ShowDialog();
            EventList[_rowOfEvent].UpdateJumpcodes();
        }

        private void buttonFinishcontest_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void buttonLastEvent_Click(object sender, EventArgs e)
        {
            if ((_rowOfEvent != -1) && (_rowOfEvent != 0))
            {
                dataGridViewEventList.Rows[_rowOfEvent].Selected = false;
                _rowOfEvent--;
                dataGridViewEventList.Rows[_rowOfEvent].Selected = true;
            }

            EventList[_rowOfEvent].UpdateJumpcodes();
            SetJumpDifficulties();
            UpdateEventJumpsLabels();
            UpdateHighScoreLabels();
            progressBar.Maximum = EventList[_rowOfEvent].JumpList.Count;
            progressBar.Value = EventList[_rowOfEvent].CurrentDiver;
        }

        private void buttonNextEvent_Click(object sender, EventArgs e)
        {
            if (_rowOfEvent != (dataGridViewEventList.Rows.Count - 1))
            {
                dataGridViewEventList.Rows[_rowOfEvent].Selected = false;
                _rowOfEvent++;
                dataGridViewEventList.Rows[_rowOfEvent].Selected = true;
            }

            EventList[_rowOfEvent].UpdateJumpcodes();
            SetJumpDifficulties();
            UpdateEventJumpsLabels();
            UpdateHighScoreLabels();
            progressBar.Maximum = EventList[_rowOfEvent].JumpList.Count;
            progressBar.Value = EventList[_rowOfEvent].CurrentDiver;
        }

        private void buttonChangejumpcode_Click(object sender, EventArgs e)
        {
            var formSetJumpCode = new FormSetJumpcodes();
            var radiobuttonpressed = CheckRadiobutton() + 1;
            formSetJumpCode.TotalAmountOfRounds = EventList[_rowOfEvent].Rounds;
            formSetJumpCode.CurrentRound =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + radiobuttonpressed].Round;
            formSetJumpCode.labelName.Text =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + radiobuttonpressed].DiverName;
            formSetJumpCode.labelPersonalCodeNumber.Text =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + radiobuttonpressed].DiverId;
            formSetJumpCode.JumpAppendixList = EventList[_rowOfEvent].Jumpappendix;
            formSetJumpCode.JumpHeight = EventList[_rowOfEvent].JumpHeight;

            for (var i = 0; i < EventList[_rowOfEvent].DiverList.Count; ++i)
            {
                if (EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + radiobuttonpressed].DiverId ==
                    EventList[_rowOfEvent].DiverList[i].Diver.PersonalCodeNumber)
                {
                    formSetJumpCode.ListOfJumpCodesAndStyles = EventList[_rowOfEvent].DiverList[i].JumpCodesandStyle;
                    i = EventList[_rowOfEvent].DiverList.Count + 1;
                }
            }

            formSetJumpCode.ShowDialog();
            EventList[_rowOfEvent].UpdateJumpcodes();
        }

        private void buttonShowjumpinformation_Click(object sender, EventArgs e)
        {
            var formJumpInformation = new FormJumpInformation();
            var radiobuttonpressed = CheckRadiobutton() + 1;
            formJumpInformation.labelDiverNameCurrent.Text =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + radiobuttonpressed].DiverName;
            formJumpInformation.labelJumpCurrent.Text =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + radiobuttonpressed].JumpId
                    .ToString();
            formJumpInformation.labelStyleCurrent.Text =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + radiobuttonpressed].Style;
            formJumpInformation.labelDifficultyCurrent.Text =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + radiobuttonpressed].Difficulty
                    .ToString();
            double maxScore = 0;
            maxScore =
                EventList[_rowOfEvent].JumpList[EventList[_rowOfEvent].CurrentDiver + radiobuttonpressed].Difficulty*30;

            formJumpInformation.labelMaximumPointsnumber.Text = Math.Round(maxScore, 1).ToString();

            formJumpInformation.ShowDialog();
        }

        private void buttonJudgeInformation_Click(object sender, EventArgs e)
        {
            var formShowJudges = new FormShowJudges();
            formShowJudges.dataGridViewContestJudgeRegister.DataSource = EventList[_rowOfEvent].JudgeList;
            formShowJudges.Show();
        }

        private void FormStartEvent_KeyDown(object sender, KeyEventArgs e)
        {
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Tab)
            {
                labelInvisible0.Visible = !labelInvisible0.Visible;
                labelInvisible1.Visible = !labelInvisible1.Visible;
                labelInvisible2.Visible = !labelInvisible2.Visible;
                labelInvisible3.Visible = !labelInvisible3.Visible;
                labelInvisible4.Visible = !labelInvisible4.Visible;
                labelInvisible5.Visible = !labelInvisible5.Visible;
                labelInvisible6.Visible = !labelInvisible6.Visible;
                labelInvisible7.Visible = !labelInvisible7.Visible;
                labelInvisible8.Visible = !labelInvisible8.Visible;
                labelInvisible9.Visible = !labelInvisible9.Visible;
                labelInvisible10.Visible = !labelInvisible10.Visible;
                labelInvisible11.Visible = !labelInvisible11.Visible;
                labelInvisible12.Visible = !labelInvisible12.Visible;
                labelInvisible13.Visible = !labelInvisible13.Visible;
                labelInvisible14.Visible = !labelInvisible14.Visible;
                labelInvisible15.Visible = !labelInvisible15.Visible;
                labelInvisible16.Visible = !labelInvisible16.Visible;
                labelInvisible17.Visible = !labelInvisible17.Visible;
                labelInvisible18.Visible = !labelInvisible18.Visible;
                labelInvisible19.Visible = !labelInvisible19.Visible;
                labelInvisible20.Visible = !labelInvisible20.Visible;
                labelInvisible21.Visible = !labelInvisible21.Visible;
                labelInvisible22.Visible = !labelInvisible22.Visible;
                labelInvisible23.Visible = !labelInvisible23.Visible;
                labelinvisibleP.Visible = !labelinvisibleP.Visible;
            }

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.Right)
                buttonNextEventjump.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.Left)
                buttonLastEventjump.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.Enter)
                buttonSetJudgepoints.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.ShiftKey)
                buttonGetResult.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.H)
                buttonChangejumpcode.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.S)
                buttonPrintHighscore.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.Escape)
                buttonFinishcontest.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.F1)
                buttonLastEvent.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.F2)
                buttonNextEvent.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.E)
                buttonChangejumpcode.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.I)
                buttonShowjumpinformation.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.D)
                buttonJudgeInformation.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.F10)
                buttonShowHighscore.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.D1)
                radioButtonjumpnext.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.D2)
                radioButtonJump0.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.D3)
                radioButtonJump1.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.D4)
                radioButtonJump2.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.D5)
                radioButtonJump3.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.D6)
                radioButtonJump4.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.D7)
                radioButtonJump5.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.D8)
                radioButtonJump6.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.D9)
                radioButtonJump7.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.D0)
                radioButtonJump8.PerformClick();

            if ((ModifierKeys == Keys.Control) & e.KeyCode == Keys.Oemplus)
                radioButtonJump9.PerformClick();

            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.P)
                textBoxD1.Select();

            if (e.KeyCode == Keys.Escape)
                buttonFinishcontest.PerformClick();
        }

        private void buttonPrintHighscore_Click(object sender, EventArgs e)
        {
            var fontBlackBold36 = new Font(iTextSharp.text.Font.NORMAL, 36f, iTextSharp.text.Font.BOLD,
                BaseColor.BLACK);
            var fontBlackBold16 = new Font(iTextSharp.text.Font.NORMAL, 16f, iTextSharp.text.Font.BOLD,
                BaseColor.BLACK);
            var fontBlackNormal12 = new Font(iTextSharp.text.Font.NORMAL, 12f, iTextSharp.text.Font.NORMAL,
                BaseColor.BLACK);

            string[] diver =
            {
                "",
                labelHSdivername1.Text,
                labelHSdivername2.Text,
                labelHSdivername3.Text,
                labelHSdivername4.Text,
                labelHSdivername5.Text,
                labelHSdivername6.Text,
                labelHSdivername7.Text,
                labelHSdivername8.Text,
                labelHSdivername9.Text,
                labelHSdivername10.Text
            };

            string[] scoreDifference =
            {
                "",
                "",
                labelHSdif2.Text,
                labelHSdif3.Text,
                labelHSdif4.Text,
                labelHSdif5.Text,
                labelHSdif6.Text,
                labelHSdif7.Text,
                labelHSdif8.Text,
                labelHSdif9.Text,
                labelHSdif10.Text
            };

            string[] score =
            {
                "",
                labelHSResult1.Text,
                labelHSResult2.Text,
                labelHSResult3.Text,
                labelHSResult4.Text,
                labelHSResult5.Text,
                labelHSResult6.Text,
                labelHSResult7.Text,
                labelHSResult8.Text,
                labelHSResult9.Text,
                labelHSResult10.Text
            };

            var dt = Convert.ToDateTime(Date);

            string fileName = ContestName + EventList[_rowOfEvent].Bracket + EventList[_rowOfEvent].JumpHeight +
                       EventList[_rowOfEvent].Gender;;
            var document = new Document(PageSize.LETTER, 10, 10, 42, 35);
            var writer = PdfWriter.GetInstance(document, new FileStream(fileName + ".pdf", FileMode.Create));

            document.Open();

            var title = new Paragraph("TOPPLISTA", fontBlackBold36);
            title.Alignment = Element.ALIGN_CENTER;

            var table = new PdfPTable(4);
            PdfPCell cell;
            cell = new PdfPCell(new Phrase(labelHSManOrWomen.Text, fontBlackBold16));
            table.AddCell(cell);
            cell = new PdfPCell(new Phrase(labelHSheight.Text, fontBlackBold16));
            table.AddCell(cell);
            cell = new PdfPCell(new Phrase(labelHSklass.Text, fontBlackBold16));
            table.AddCell(cell);
            cell = new PdfPCell(new Phrase(labelHSSingleOrCouple.Text, fontBlackBold16));
            table.AddCell(cell);

            table.AddCell("Position");
            table.AddCell("Namn");
            table.AddCell("Poängskillnad");
            table.AddCell("Poäng");

            for (var i = 1; i <= 10; i++)
            {
                cell = new PdfPCell(new Phrase(i + ".", fontBlackNormal12));
                cell.HorizontalAlignment = 2;
                table.AddCell(cell);

                table.AddCell(diver[i]);

                cell = new PdfPCell(new Phrase(scoreDifference[i], fontBlackNormal12));
                cell.HorizontalAlignment = 2;
                table.AddCell(cell);

                table.AddCell(score[i]);
                ;
            }

            var information = new Paragraph
                (
                ContestName + "           " + dt.Year + "-" + dt.Month + "-" + dt.Day
                );
            information.Alignment = Element.ALIGN_CENTER;

            document.Add(title);
            document.Add(table);
            document.Add(information);
            document.Close();
            MessageBox.Show(@"Topplista har skrivits ut med namnet " + fileName);
        }

        public bool CheckTextboxes()
        {
            int[] tempTexBoxArray =
            {
                0,
                0,
                0,
                0,
                textBoxD4.TextLength,
                textBoxD5.TextLength,
                textBoxD6.TextLength,
                textBoxD7.TextLength,
                textBoxD8.TextLength,
                textBoxD9.TextLength,
                textBoxD10.TextLength,
                textBoxD11.TextLength
            };

            if (EventList[_rowOfEvent].JudgeList.Count >= 3)
            {
                if (textBoxD1.TextLength == 0 || textBoxD2.TextLength == 0 || textBoxD3.TextLength == 0)
                    return false;
            }

            for (var i = 4; i <= 11; i++)
            {
                if ((EventList[_rowOfEvent].JudgeList.Count >= (i + 1)) &&
                    (tempTexBoxArray[i] == 0 || tempTexBoxArray[i + 1] == 0))
                    return false;
            }

            return true;
        }

        public int CheckRadiobutton()
        {
            bool[] radioButtonJump =
            {
                radioButtonJump0.Checked,
                radioButtonJump1.Checked,
                radioButtonJump2.Checked,
                radioButtonJump3.Checked,
                radioButtonJump4.Checked,
                radioButtonJump5.Checked,
                radioButtonJump6.Checked,
                radioButtonJump7.Checked,
                radioButtonJump8.Checked,
                radioButtonJump9.Checked
            };

            if (radioButtonjumpnext.Checked)
                return 0;

            for (var i = 0; i <= 9; i++)
            {
                if (radioButtonJump[i])
                    return i + 1;
            }

            return 0;
        }

        private void labelInvisible1_Click(object sender, EventArgs e)
        {
        }

        public void SetJumpDifficulties()
        {

            for (var j = 0; j < EventList[_rowOfEvent].JumpList.Count; ++j)
            {
                EventList[_rowOfEvent].JumpList[j].Difficulty =
                    EventList[_rowOfEvent].Jumpappendix.FindDifficulty(EventList[_rowOfEvent].JumpList[j].JumpId,
                        EventList[_rowOfEvent].JumpList[j].Style, EventList[_rowOfEvent].JumpHeight);
            }
        }

        private void dataGridViewEventList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        #region UDP server variabler

        private delegate void TextBoxDelegate(string message);

        private static readonly UdpClient Server = new UdpClient(9059);
        private static readonly List<Client> List = new List<Client>();
        public List<string> Voted = new List<string>();

        #endregion

        #region UDP get points

        public void GetPoints()
        {
            #region set .

            var customCulture = (CultureInfo) Thread.CurrentThread.CurrentCulture.Clone();
            customCulture.NumberFormat.NumberDecimalSeparator = ".";

            Thread.CurrentThread.CurrentCulture = customCulture;

            #endregion

            while (true)
            {
                var remote = new IPEndPoint(IPAddress.Any, 9058);
                var data = Server.Receive(ref remote);
                var s = Encoding.ASCII.GetString(data);
                var fi = new FileInfo(@"log4net.xml");
                XmlConfigurator.Configure(fi);
                Logger.Info(remote + @" connectade till servern med meddelandet: " + s);

                Console.WriteLine(@"Data från: " + remote + @"   " + s);

                if (s == "TEST")
                {
                    var readIPandPort = remote.ToString();
                    var iPandPort = readIPandPort.Split(':');
                    var c = new Client(iPandPort[0], Convert.ToInt16(iPandPort[1]));
                        //Gör ett event som anropar konstruktorn.
                    Server.Send(new byte[] {1}, 1, remote);
                    var connected = 0;
                    for (var i = List.Count - 1; i >= 0; --i)
                    {
                        if (List[i] == c)
                            connected++;
                    }
                    if (connected == 0)
                    {
                        List.Add(c);
                    }
                }
                else
                {
                    var message = s.Split('|');
                    if (message[1] == "Points@truekil")
                    {
                        var exist = false;
                        var alreadyVoted = false;
                        for (var i = 0; i < Voted.Count; ++i)
                        {
                            if (Voted[i] == message[0])
                            {
                                alreadyVoted = true;
                                break;
                            }
                        }
                        if (!alreadyVoted)
                        {
                            //Bearbeta poängen.
                            for (var i = 0; i < EventList[_rowOfEvent].JudgeList.Count; ++i)
                            {
                                if (EventList[_rowOfEvent].JudgeList[i].PersonalCodeNumber == message[0])
                                {
                                    exist = true;
                                    Voted.Add(EventList[_rowOfEvent].JudgeList[i].PersonalCodeNumber);
                                    break;
                                }
                            }
                            if (exist)
                            {
                                try
                                {
                                    if (textBoxD1.InvokeRequired)
                                    {
                                        TextBoxDelegate d = SetTextBoxText;
                                        Invoke(d, Convert.ToString(message[2]));
                                    }
                                }
                                catch (FormatException)
                                {
                                }
                                catch (OverflowException oe)
                                {
                                    Logger.Error(oe.Source + " " + oe.Message);
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine(@"Felaktig data från " + remote);
                    }
                }
            }
        }

        private void SetTextBoxText(string text)
        {
            var tempList = new BindingList<TextBox>();
            tempList.Add(textBoxD1);
            tempList.Add(textBoxD2);
            tempList.Add(textBoxD3);
            tempList.Add(textBoxD4);
            tempList.Add(textBoxD5);
            tempList.Add(textBoxD6);
            tempList.Add(textBoxD7);
            tempList.Add(textBoxD8);
            tempList.Add(textBoxD9);
            tempList.Add(textBoxD10);
            tempList.Add(textBoxD11);

            for (var i = 0; i <= 9; i++)
            {
                if (tempList[i].Text == "")
                {
                    tempList[i].Text = Convert.ToString(text);
                    break;
                }
            }
        }

        #endregion

        private void labelHSResult1_Click(object sender, EventArgs e)
        {

        }
    }
}